#include <iostream>
#include <fstream>
#include "boost/filesystem.hpp"
#include "boost/filesystem/fstream.hpp"
#define BOOST_FILESYSTEM_NO_DEPRECATED

namespace fs = boost::filesystem;

int main(int argc, char** argv) {
    fs::fstream file;

    unsigned long file_count = 0;
    fs::path full_path( fs::initial_path<fs::path>());

    if (argc > 1)
        full_path = fs::system_complete(fs::path( argv[1] ));
    else
        std::cout << "\nusage:   sio [path]" << std::endl;

    const unsigned numReqs = argc - 1;

    if (!fs::exists( full_path )) {
        std::cout << "\nNot found: " << full_path.file_string() << std::endl;
        return 1;
    }

    if (fs::is_directory( full_path )) {
        std::cout << "\nIn directory: " << full_path.directory_string() << "\n\n";
        fs::directory_iterator end_iter;
        for ( fs::directory_iterator dir_itr(full_path); dir_itr != end_iter; ++dir_itr) {
            try {
                if (fs::is_regular_file( dir_itr->status())) {
                    ++file_count;
                    std::cout << "opening file " << dir_itr->path().filename() << "\n";

                    file.open(dir_itr->path().filename(), std::ios::in);
                    std::cout << "file opened\n";
                    file.close();
                }
            } catch ( const std::exception & ex ) {
                std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl;
            }
        }
        std::cout << "\n" << file_count << " files\n";
    }
    return 0;
}
